package com.jiyun.leyou.controller;

import com.jiyun.leyou.pojo.Cart;
import com.jiyun.leyou.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("购物接口相关")
public class CartController {

    @Autowired
    CartService cartService;

    @PostMapping
    @ApiOperation(value = "添加购物车的接口",notes = "添加购物车")
    @ApiImplicitParam(name="cart",required = true,value = "购物车的对象")
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        // 获取数据

        // 去后台，将数据添加到指定的数据库，Redis
        cartService.addCart(cart);

        return ResponseEntity.ok().build();
    }

    @GetMapping("query")
    @ApiOperation(value = "查询购物车的操作",notes = "查询所有购物车")
    public ResponseEntity<List<Cart>> queryCart(){
        List<Cart> list = cartService.queryCart();

        return ResponseEntity.ok(list);
    }

    @PutMapping
    @ApiOperation(value = "修改购物车的接口",notes = "修改购物车")
    @ApiImplicitParam(name="cart",required = true,value = "购物车的对象")
    public ResponseEntity<Void> putCart(@RequestBody Cart cart){
        cartService.putCart(cart);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{skuId}")
    @ApiOperation(value = "删除购物车的接口",notes = "添加购物车")
    @ApiImplicitParam(name="skuId",required = true,value = "购物车对象的id")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId){
        cartService.deleteCart(skuId);

        return ResponseEntity.ok().build();
    }


}
